<template>
  <div class="container">
    <div class="wrapper">
      <div style="font-weight: bold; font-size: 24px; text-align: center; margin-bottom: 30px; color: #1450aa">欢 迎 登 录
      </div>
      <el-form :model="form" ref="formRef" :rules="rules">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" size="large" v-model="form.username" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" size="large" v-model="form.password" placeholder="请输入密码" show-password />
        </el-form-item>
        <el-form-item>
          <el-button size="large" type="primary" style="width: 100%" @click="handleLogin">登 录</el-button>
        </el-form-item>
      </el-form>
      <div style="text-align: center;">
        没有账号？ <a href="/register">去注册</a>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          if (this.form.username === 'admin' && this.form.password === '123456') {
            this.$message.success('登录成功');
            this.$router.push('/');
            const res = {
              id: 1,
              username: this.form.username,
              name: '管理员'
            }
            localStorage.setItem('system-user', JSON.stringify(res));
          } else {
            this.$message.error('用户名或密码错误');
          }
        }
        // 调用后台的接口
        // request.post('/login', this.form).then(res => {
        //   if (res.code === '200') {
        // this.$message.success("登录成功");
        // this.$router.push('/');
        //     localStorage.setItem('system-user', JSON.stringify(res.data));
        //   } else {
        //     this.$message.error(res.msg);
        //   }
        // }).catch(error => {
        //   console.error(error);
        // });
        // }
      });
    }
  }
}
</script>

<style scoped>
.container {
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(to top, #7f7fd5, #86a8e7, #91eae4);
  background-size: cover;
}

.wrapper {
  width: 350px;
  padding: 50px 30px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, .1);
  background-color: rgba(255, 255, 255, .5);
}
</style>